/**
 * 物理视图组件
 * 
 * 职责：
 * 1. 缓存物理相关组件（RigidBody2D, Collider2D）
 * 2. 提供物理操作的便捷方法
 * 
 * 适用场景：
 * - 需要物理模拟的实体（角色、子弹、物理道具等）
 * - 需要碰撞检测的实体
 * 
 * 内存占用：~56 bytes
 * - 基类: ~40 bytes
 * - _rigidbody: 8 bytes
 * - _collider: 8 bytes
 * 
 * @author AI Assistant
 * @date 2025-11-29
 */

import { ecs } from "../../../../core/ecs/ECS";
import { RigidBody2D, Collider2D, Vec2 } from "cc";
import { ViewComponentBase } from "./ViewComponentBase";

/**
 * 物理视图组件
 */
@ecs.register('ViewPhysics', false)
export class ViewComponentPhysics extends ViewComponentBase {
    // ========== 懒加载缓存 ==========
    
    /** RigidBody2D 组件（2D 物理刚体）- 懒加载 */
    private _rigidbody: RigidBody2D | null | undefined = undefined;
    
    /** Collider2D 组件（2D 碰撞器）- 懒加载 */
    private _collider: Collider2D | null | undefined = undefined;
    
    // ========== 懒加载 Getter ==========
    
    /**
     * 获取 RigidBody2D 组件（懒加载）
     */
    get rigidbody(): RigidBody2D | null {
        if (this._rigidbody === undefined) {
            this._rigidbody = this.node?.getComponent(RigidBody2D) || null;
        }
        return this._rigidbody;
    }
    
    /**
     * 获取 Collider2D 组件（懒加载）
     */
    get collider(): Collider2D | null {
        if (this._collider === undefined) {
            this._collider = this.node?.getComponent(Collider2D) || null;
        }
        return this._collider;
    }
    
    /**
     * 重置组件
     */
    reset(): void {
        super.reset();
        this._rigidbody = undefined;
        this._collider = undefined;
    }
    
    // ========== 便捷方法 ==========
    
    /**
     * 应用力
     */
    applyForce(force: Vec2, point?: Vec2): void {
        if (this.rigidbody) {
            if (point) {
                this.rigidbody.applyForce(force, point, true);
            } else {
                this.rigidbody.applyForceToCenter(force, true);
            }
        }
    }
    
    /**
     * 应用冲量
     */
    applyImpulse(impulse: Vec2, point?: Vec2): void {
        if (this.rigidbody) {
            if (point) {
                this.rigidbody.applyLinearImpulse(impulse, point, true);
            } else {
                this.rigidbody.applyLinearImpulseToCenter(impulse, true);
            }
        }
    }
    
    /**
     * 设置线性速度
     */
    setLinearVelocity(velocity: Vec2): void {
        if (this.rigidbody) {
            this.rigidbody.linearVelocity = velocity;
        }
    }
    
    /**
     * 获取线性速度
     */
    getLinearVelocity(): Vec2 | null {
        return this.rigidbody ? this.rigidbody.linearVelocity : null;
    }
    
    /**
     * 设置角速度
     */
    setAngularVelocity(velocity: number): void {
        if (this.rigidbody) {
            this.rigidbody.angularVelocity = velocity;
        }
    }
    
    /**
     * 启用/禁用碰撞器
     */
    setColliderEnabled(enabled: boolean): void {
        if (this.collider) {
            this.collider.enabled = enabled;
        }
    }
    
    /**
     * 检查是否有物理组件
     */
    hasPhysicsComponent(): boolean {
        return this.rigidbody !== null || this.collider !== null;
    }
}

